import React, { useState, useRef } from 'react';
import { message } from 'antd';
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table';
import services from '@/services/files';

const { fetchScores } = services.PositionScore;

const PositionPage: React.FC = () => {
  const [tableLoading, setTableLoading] = useState(false);
  const [scoreData, setScoreData] = useState<any[]>([]);
  
  // 年份选项配置
  const yearOptions = {
    2025: '2025年',
    2024: '2024年',
    2023: '2023年',
    2022: '2022年',
    2021: '2021年'
  };

  const defaultColumn = [
    {
      title: '选择时间',
      dataIndex: 'date',
      valueType: 'select',
      valueEnum: yearOptions,
      initialValue: '2025',
      search: true,
      hideInTable: true
    },
    { title: '排名', dataIndex: 'rank', width: 80, search: false, fixed: 'left' },
    { title: '职位代码', dataIndex: 'positionCode' },
    { title: '行测成绩', dataIndex: 'administrativeAptitudeScore', search: false, },
    { title: '申论成绩', dataIndex: 'essayScore', search: false, },
    { title: '专业成绩', dataIndex: 'professionalScore', search: false, },
    { title: '笔试成绩', dataIndex: 'writtenExamScore', search: false, },
    { title: '专业科目', dataIndex: 'professionalSubject', search: false, },
    { title: '申论类别', dataIndex: 'essayCategory', search: false, },
    { title: '划线分类', dataIndex: 'scoreCategory', search: false, },
    { title: '准考证号', dataIndex: 'examNumber', fixed: 'right' },
  ];
  
  const [scoreColumns, setScoreColumns] = useState<ProColumns<any>[]>(defaultColumn);
  const actionRef = useRef<ActionType>();

  // 加载职位数据
  const loadPositions = async (params: any = {}) => {
    setTableLoading(true);
    try {
      const res = await fetchScores({
        province: '安徽省',
        city: '宿州市',
        positionCode: '1',
        ...params,
      });
      setScoreData(res.data || []);
      // const cols = (res.fractionNameKeyMap || []).map((col: any) => ({
      //   title: col.name || col,
      //   dataIndex: col.key || col,
      //   ...col,
      // }));
      // setScoreColumns([...defaultColumn, ...cols]); // 保留默认列并添加新列
    } catch (e) {
      message.error('获取成绩数据失败');
    } finally {
      setTableLoading(false);
    }
  };

  return (
    <div>
      <ProTable
        rowKey="positionCode"
        columns={scoreColumns}
        dataSource={scoreData}
        loading={tableLoading}
        virtual
        scroll={{
          x: scoreColumns.reduce((sum, col) => sum + (col.width || 120), 0),
          y: window.innerHeight - 290,
        }}
        toolbar={{
          settings: () => []
        }}
        actionRef={actionRef}
        pagination={false}
        search={{ labelWidth: 'auto' }}
        request={async (params) => {
          await loadPositions(params);
          return { data: scoreData, success: true };
        }}
      />
    </div>
  );
};

export default PositionPage;